/**
 *
 * Copyright (c) 2014 BinKylin Studio.
 *
 * Author: TBinkylin
 * Created Date: 2014-03-01
 * 
 * 	version					author					remark
 * 	1.0						benoit				
 */
package net.binkylin.common.utils;

import java.math.BigDecimal;

public class NumericUtil {

	public static String formatLong(Long longValue) {
		if (longValue == null)
			return "";
		return String.valueOf(longValue.longValue());
	}

	public static Long parseLong(String longStr) {
		if (longStr == null || longStr.trim().equalsIgnoreCase(""))
			return null;
		return new Long(longStr);
	}

	public static String formatDouble(Double doubleValue) {
		if (doubleValue == null)
			return "";
		return String.format("%.2f", doubleValue);
	}

	public static Double parseDouble(String doubleStr) {
		if (doubleStr == null || doubleStr.trim().equalsIgnoreCase(""))
			return null;
		return new Double(doubleStr);
	}

	/**
	 * Checks if a string value is a valid number. A valid number should only
	 * contain digits and decimal point (if it is a double or long value).
	 * <p/>
	 * 
	 * @param value
	 *            The String value to check if it is a valid number
	 * @return true if the value is a valid number; false otherwise
	 */
	public static boolean isNumber(String value) {
		try {
			new BigDecimal(value);
		} catch (NumberFormatException e) {
			return false;
		}
		return true;
	}

}
